#include "gameselect.h"
#include "gamehall.h"
#include "gameroom.h"
#include "utility.hpp"
#include "mainwindow.h"
#include <QIcon>
#include <QPainter>
#include <QPushButton>
#include <QVBoxLayout>
#include <QFrame>
#include <QSound>

gameSelect::gameSelect(QWidget *parent) : QWidget(parent)
{
    //读取qss文件
    const QString &style = utility::ReadFile("C:/Users/LKM/Code/snake-game/Snake/qss/gameHall/QPushButton.txt");

    //设置标题
    this->setWindowTitle("游戏大厅");

    //设置窗口大小
    this->resize(1000,800);

    //设置ICon
    this->setWindowIcon(QIcon(":/resource/ico.png"));

    //创建游戏大厅的四个按钮，并设置相关属性
    QPushButton *simpleButton = new QPushButton(this);
    QPushButton *normalButton = new QPushButton(this);
    QPushButton *hardButton = new QPushButton(this);
    QPushButton *historyButton = new QPushButton(this);
    simpleButton->setStyleSheet(style);
    normalButton->setStyleSheet(style);
    hardButton->setStyleSheet(style);
    historyButton->setStyleSheet(style);
    simpleButton->setText("简单模式");
    normalButton->setText("普通模式");
    hardButton->setText("困难模式");
    historyButton->setText("历史记录");

    //创建一个游戏房间的窗口
    gameRoom *gameroom = new gameRoom;

    //游戏难度选择按钮绑定槽函数
    connect(simpleButton,&QPushButton::clicked,[=](){
         //关闭当前窗口
         this->close();

         //设置gameRoom窗口的大小
         gameroom->setGeometry(this->geometry());

         //设置蛇的移动速度
         gameroom->setMovementSpeed(200);

         //设置游戏难度
         gameroom->setGameDifficulty(Difficulty::simple);

         //显示新窗口
         gameroom->show();

         //播放点击音效
         QSound::play(":/resource/clicked.wav");
    });

    connect(normalButton,&QPushButton::clicked,[=](){
         //关闭当前窗口
         this->close();

         //设置gameRoom窗口的大小
         gameroom->setGeometry(this->geometry());

         //设置蛇的移动速度
         gameroom->setMovementSpeed(100);

         //设置游戏难度
         gameroom->setGameDifficulty(Difficulty::mormal);

         //显示新窗口
         gameroom->show();

         //播放点击音效
         QSound::play(":/resource/clicked.wav");
    });

    connect(hardButton,&QPushButton::clicked,[=](){
         //关闭当前窗口
         this->close();

         //设置gameRoom窗口的大小
         gameroom->setGeometry(this->geometry());

         //设置蛇的移动速度
         gameroom->setMovementSpeed(50);

         //设置游戏难度
         gameroom->setGameDifficulty(Difficulty::hard);

         //显示新窗口
         gameroom->show();

         //播放点击音效
         QSound::play(":/resource/clicked.wav");
    });

    connect(historyButton,&QPushButton::clicked,[=](){
        //创建一个窗口
        MainWindow *mainWindow = new MainWindow;
        mainWindow->setAttribute(Qt::WA_DeleteOnClose);
        mainWindow->show();

    });


    //创建一个布局管理器，管理四个按钮
    QFrame *frame = new QFrame(this);//通过这个frame来控制布局管理器的大小与位置
    frame->setGeometry(300,200,400,400);
    QVBoxLayout *vlayout = new QVBoxLayout(frame); //将布局管理器添加进frame中
    vlayout->addWidget(simpleButton);
    vlayout->addWidget(normalButton);
    vlayout->addWidget(hardButton);
    vlayout->addWidget(historyButton);


    //创建返回按钮
    QPushButton *backButton = new QPushButton(this);
    backButton->setIcon(QIcon(":/resource/back.png"));
    backButton->resize(50,50);
    backButton->move(900,700);



    //为返回按钮绑定信号槽
    connect(backButton,&QPushButton::clicked,[=](){
        //关闭当前窗口
        this->close(); //并不会立马释放内存

        //创建一个gameHall窗口
        gameHall *gamehall = new gameHall;

        //显示窗口
        gamehall->show();

        //播放点击音效
        QSound::play(":/resource/clicked.wav");
    });
}

//重写绘制事件绘制游戏大厅的背景
void gameSelect::paintEvent(QPaintEvent *event)
{
     //消除警告
     (void)event;

      //创建绘画者
     QPainter painter(this); //这里的this,不是指定父节点挂到对象树上，而是指定绘画的设备，这里我们要往this,也就是widget上绘画

     //创建图片对象
     QPixmap pixmap(":/resource/game_select.png"); //要绘制的背景图片

     //开始绘制
     painter.drawPixmap(0,0,this->width(),this->height(),pixmap); //绘制出图片的大小需要和窗口的宽高一致

}
